/**
 *
 * 拍照获取图片   appendByCamera
 * 调用 appendByCamera方法即可，
 * 需要修改上传地址的ip:port
 */
var server="http://192.168.199.243:8080/Manage/Mobile/uploadify";
//server = 'http://demo.dcloud.net.cn/helloh5/uploader/upload.php';
var files=[];
// 上传文件
function upload(){
    if(files.length<=0){
        plus.nativeUI.alert("没有添加上传文件！");
        return;
    }

    var wt=plus.nativeUI.showWaiting("开始上传...");
    var task=plus.uploader.createUpload(server,
        {method:"POST"},
        function(t,status){ //上传完成
            if(status==200){
                plus.storage.setItem("uploader",t.responseText);
                mui('#photo_minx')[0].src = t.responseText;
                mui('#photo_minx_hi')[0].value = t.responseText;

                wt.close();
            }else{
                plus.nativeUI.alert("上传失败："+status);
                wt.close();
            }
        }
    );
    task.addData("uid",getUid());
    for(var i=0;i<files.length;i++){
        var f=files[i];
        task.addFile(f.path,{key:f.name});
        //compressImage(f.path, task);
    }
    task.start();
}
// 拍照添加文件
function appendByCamera(){
    plus.camera.getCamera().captureImage(function(p){
        //appendFile(p);
        compressImage(p);
    });
}
function appendByGallery(){
    plus.gallery.pick(function(p){
        compressImage(p);

    });
}
// 添加文件
var index=1;
function appendFile(p){
    files.push({name:"uploadkey"+index,path:p});
    index++;
    upload();
}
// 产生一个随机数
function getUid(){
    return Math.floor(Math.random()*100000000+10000000).toString();
}

//压缩图片
function compressImage(path){
    var dst = '_doc/'+getUid()+'.jpg';
    console.log("开始压缩");
    plus.zip.compressImage({
            src:path,
            dst:dst,
            quality:20,
            format:'jpg'
            //overwrite:true,
        },
        function() {
            console.log("Compress success!");
            appendFile(dst);
        },function(error) {
            console.log("Compress error!");
        }
    );
}